$(document).ready(function() {
	var oTable;
	/**
	* Initialize the table data.
	*/
	oTable = $('#dataTables-example').dataTable(
		{
			"bProcessing" : true,
			"sAjaxSource" : "getAllUsers.html",
			"sAjaxDataProp" : "",
			"aoColumns" : [ {"mData" : "userId"},
				                {"mData" : "userId"}, 
				                {"mData" : "userAccount"}, 
				                {"mData" : "userName"}, 
				                {"mData" : "userEnabled"}, 
				                {"mData" : "userId"} 
				              ],
			"aoColumnDefs" : [ {
								"aTargets" : [ 0 ],
								"mData" : "选择",
								"mRender" : function(data,type, full) {
										    var contentView="";
										    contentView+="<input type='checkbox' name='userSelected' value='"+data+"'/>"
										    return contentView;
										    }
									},
									{
									"aTargets" : [ 4 ],
									"mData" : "操作",
									"mRender" : function(data,type, full) {
													var viewContent = "";
													if(data==1){
														viewContent+="<span class='label label-success'>启用</span>";
													}else{
														viewContent+="<span class='label label-warning'>禁用</span>";	
													}
													return viewContent;
												}
									},
									{
										"aTargets" : [ 5 ],
										"mData" : "操作",
										"mRender" : function(data,type, full) {
														var operations = "";
														operations += ' <button type="button" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></button>';
														operations += ' <button type="button" class="btn btn-success btn-circle"><i class="fa fa-link"></i></button>';
														operations += ' <button type="button" class="btn btn-info btn-circle"><i class="fa fa-check"></i></button>';
														operations += ' <button type="button" class="btn btn-warning btn-circle"><i class="fa fa-times"></i></button>';
														operations += ' <button type="button" class="btn btn-danger btn-circle"><i class="fa fa-heart"></i></button>';
														return operations;
													}
									} ],
				"oLanguage" : {
								 "sSearch" : "全文搜索:"
							  },
				fnInitComplete : function() {
											/**
											 * 数据下拉框初始化
											 */
									$("thead td ").each(function(i) {
														var selectInputs = $(this).children("select");
														var selectInput = null;
														if (selectInputs != null&& selectInputs.length != 0) {
															selectInput = selectInputs[0];
														}
														if (selectInput != null) {
															$('select',this).change(function() {
																			oTable.fnFilter($(this).find("option:selected").text(),i);
																		});
														}
														});
									$("#dataTables-example tbody tr").on("click", function(i) {
										        if ( $(this).hasClass('row_selected') ) {
										            $(this).removeClass('row_selected');
										        }
										        else {
										            oTable.$('tr.row_selected').removeClass('row_selected');
										            $(this).addClass('row_selected');
										        }
										        var user = oTable.fnGetData( $(this).index());
										        $("#userId").attr("value",user.userId);
										        $("#userName").attr("value",user.userName);
										        $("#userAccount").attr("value",user.userAccount);
										        $("#userEnabled").val(user.userEnabled);
										        $("#userDesc").attr("value",user.userDesc);
										        loaderUserRoles(user.userId);
										        loadUserDetailForUpdate(user.userId);
										    });
										}
									});
					// Add Check box after every row
					
					$("thead input").keyup(
							function() {
								/* Filter on the column (the index) of this element */
								oTable.fnFilter(this.value, oTable.oApi._fnVisibleToColumnIndex(oTable.fnSettings(), $("thead input").index(this)));
							});
					$("thead input").each(function(i) {
						this.initVal = this.value;
					});

					$("thead input").focus(function() {
						if (this.className == "search_init") {
							this.className = "";
							this.value = "";
						}
					});
					$("thead input").blur(function(i) {
						if (this.value == "") {
							this.className = "search_init";
							this.value = this.initVal;
						}
					});
					
				});
/**
 * 用户对象定义
 * @param userId
 * @param userAccount
 * @param userName
 * @param userPassword
 * @param userEnabled
 * @param userDesc
 * @returns
 */
function User(userId,userAccount,userName,userPassword,userEnabled,userDesc){
	this.userId=userId;
	this.userAccount=userAccount;
	this.userName=userName;
	this.userPassword=userPassword;
	this.userEnabled=userEnabled;
	this.userDesc=userDesc;
}

/**
 * 角色对象定义
 * @param roleId
 * @param roleName
 * @param roleEnabled
 * @param roleDesc
 * @param defaultRoleUrl
 * @returns
 */
function Role(roleId,roleName,roleEnabled,roleDesc,defaultRoleUrl){
	this.roleId=roleId;
	this.roleName=roleName;
	this.roleEnabled=roleEnabled;
	this.roleDesc=roleDesc;
	this.defaultRoleUrl=defaultRoleUrl;
}

/**
 * 获取用户角色信息
 * @param userId
 */
function loaderUserRoles(userId){
	$("#userId").attr("value",userId);
	$("#userRolesInGroup").empty();
	$("#userRolesOutGroup").empty();
	$.ajax({
		type : "POST",
		url : "getCurrentUserRoles.html",
		data:{"userId":userId},
		success : function(data) {
			
			if(data.resultCode==0){
				var roleMap=data.data;
				var currentRoles=roleMap.currentRoles;
				var otherRoles=roleMap.otherRoles;
				for(var i=0;i<currentRoles.length;i++){
					$("#userRolesInGroup").append("<option value='"+currentRoles[i].roleId+"'>"+currentRoles[i].roleName+"</option>");
				}
				for(var j=0;j<otherRoles.length;j++){
					$("#userRolesOutGroup").append("<option value='"+otherRoles[j].roleId+"'>"+otherRoles[j].roleName+"</option>");
				}
			}
			
		},
		error : function(XMLHttpRequest, textStatus,errorThrown) {
			
		}
	});
}

function loadUserDetailForUpdate(userId){
	$.ajax({
		type : "POST",
		url : "getUserDetail.html",
		data:{"userId":userId},
		success : function(data) {		
				var user=data.data;
				if(data.resultCode==0){
				$("#userId").attr("value",user.userId);
				$("#userAccount").attr("value",user.userAccount);
				$("#userName").attr("value",user.userName);
				$("#userEnabled").attr("value",user.userEnabled);
				$("#userDesc").val(user.userDesc);
			}else{
			}
		},
		error : function(XMLHttpRequest, textStatus,errorThrown) {

		}});
}
